import { defineStore } from 'pinia'
import { getMenu } from '@/api/modules/login'
import { setToken } from '@/utils/auth'
import router from '@/router'

interface userInfo {
  isCollapse: boolean
  menuList: NMenu.IMenu[]
  token: string
}

const useMainStore = defineStore('main', {
  state: (): userInfo => {
    return {
      isCollapse: false, // 菜单折叠
      menuList: [], // 菜单列表
      token: '',
    }
  },
  // pinia 持久化
  // persist: true,
  getters: {},
  actions: {
    setCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 发起菜单请求，并将其存入到仓库中
    async setRoutes() {
      await getMenu().then((res: IResponse) => {
        this.menuList = res.data
      })
    },
    setToken(token: string) {
      this.token = token
      setToken(token)
    },

    removeMenuList() {
      this.menuList = []
    },
  },
})

export default useMainStore
